require "Common/define"
require "Logic/Config"
require "Common/functions"
require "Common/Class"

-------------------------------------------------
-- 宠物信息结构体
local PetInfoStruct = Class("PetInfoStruct")

function PetInfoStruct:ctor(id, genes, geneticMap, matingCount, cupLv,
                            cupSession, status, locked, matingEndTime, playerName,
                            petIcon)
    self.petId = id
    self.genes = genes
    self.geneticMap = geneticMap
    self.matingCount = matingCount
    self.cupLv = cupLv
    self.cupSession = cupSession
    self.IsMatingStatus = math.floor(status / 8) == 1               --请求支援拟变 1xxx
    self.IsBeautyPageantStatus = math.floor((status % 8)) / 4 == 1  --选美 x1xx
    self.IsFollowStatus = math.floor(((status % 8) % 4) / 2) == 1   --跟随 xx1x
    self.IsNormalStatus = math.floor(status % 2) == 1               --常规 xxx1
    self.IsLocked = locked == 1
    self.matingEndTime = (matingEndTime or 0) / 1000
    self.petName = PetManager.GetPetName(self.genes)
    self.playerName = playerName
    self.petIcon = petIcon
end
-------------------------------------------------
-- 拟变请求信息的结构体
local PetMatingApplyInfoStruct = Class("PetMatingApplyInfoStruct")

function PetMatingApplyInfoStruct:ctor(templatePetInfo, mutatePetInfo, fromPlayerId, fromPlayerName, fromPlayerPic, fromPlayerPicFrame, toPlayerId, toPlayerName, toPlayerPic, toPlayerPicFrame, endTime)
    self.templatePetInfo = templatePetInfo
    self.mutatePetInfo = mutatePetInfo
    self.fromPlayerId = fromPlayerId
    self.fromPlayerName = fromPlayerName
    self.fromPlayerPic = fromPlayerPic
    self.fromPlayerPicFrame = fromPlayerPicFrame
    self.toPlayerId = toPlayerId
    self.toPlayerName = toPlayerName
    self.toPlayerPic = toPlayerPic
    self.toPlayerPicFrame = toPlayerPicFrame
    self.endTime = endTime / 1000
end

-------------------------------------------------------------------
-- 他人宠物信息
local OtherPetInfoStruct = Class("OtherPetInfoStruct")

function OtherPetInfoStruct:ctor(petId, createTime, matingCount, cupLv, cupSession,
                                 matingEndTime, genes, geneticMap, playerId, playerName,
                                 petIcon, pic, framePic)
    self.petId = petId
    self.createTime = createTime
    self.matingCount = matingCount
    self.cupLv = cupLv
    self.cupSession = cupSession

    self.matingEndTime = matingEndTime / 1000
    self.genes = genes
    self.geneticMap = geneticMap
    self.playerId = playerId
    self.playerName = playerName
    self.pic = pic
    self.picFrame = framePic
    self.petIcon = petIcon
    self.petName = PetManager.GetPetName(self.genes)
end

-------------------------------------------------------------------
--遗传谱系
local GeneticMapStruct = Class("GeneticMapStruct")

function GeneticMapStruct:ctor(genes, playerName, petIcon)
    self.genes = genes
    self.playerName = playerName
    self.petIcon = petIcon
    self.petName = PetManager.GetPetName(self.genes)
end

-------------------------------------------------------------------
--[[
    族谱列表geneticMap
    3   4   5   6
    |___|   |___|
      |       |
      1       2
      |_______|
          |
         self
]]

PetManager = {}
local this = PetManager

this.CurrentPetId = 0               --宠物界面当前选中预览的宠物id
this.TemplatePetId = 0              --拟变界面的模板体
this.MutatePetId = 0                --拟变界面的拟变体
this.IsTemplatePet = true           --拟变的宠物是否为模板体
this.petLv = 0                      --宠物等级(所有宠物共用)
this.petExp = 0                     --宠物经验值
this.CurrentExp = 0
this.applies = {}                   --收到的拟变请求列表 [PetMatingApplyInfoStruct1,PetMatingApplyInfoStruct2...]
this.myApply = {}                     --我的拟变申请结构体  PetMatingApplyInfoStruct

this.MaxPetCount = 0                --宠物的最大数量(非宠物列表数)
this.MaxPetLevel = 0                --最大的宠物等级
this.MaxPetElvLevel = 0             --飞飞进化最大等级
this.PetInfoList = {}
this.NewBornPetId = 0               --拟变新出生宠物id
this.NewAddPetIds = {}               --宠物蛋新开出的宠物们
this.SelectedOthersApplyIndex = 1    --选中的他人申请索引
this.OtherPetInfo = {}
this.AllParts = nil;                  --key：部件类型，value：该类型所有部件；
this.saveTryPartDataGroup = {}        --当前试组属性
this.EvoLevelExp = 0;                 --当前进化总经验
this.EvoLevel = 0;                    --当前进化等级
this.lockParts = {};                  --当前锁定部位id
this.isEvoLvUp = 0;                   --宠物进化是否升级 0not 1yes
this.showBattleFightPetInfo = nil            --战斗中要显示的宠物
this.isQuickMating = false;            --快速拟变标签
this.partAttrData = {}
this.petPartTBdata = {}
this.petPartTBSkill = {}
this.petPartAllLv = 0
this.petPartSkillByGroup = nil
function this.init()
    this.NewAddPetIds = {}
    this.MaxPetCount = G.dataTable["t_global"][tostring(119)].f_int_value
    this.petPartTBdata = G.dataTable["t_petgrowth"]
    this.petPartTBSkill = G.dataTable["t_petskill"]
    local mergeConfig = G.dataTable["t_elvesmerge"]
    local evoConfig = G.dataTable["t_elvesevolevel"]
    local i = 1
    local curMergeConfig = mergeConfig[tostring(i)]
    while curMergeConfig do
        this.MaxPetLevel = i
        i = i + 1
        curMergeConfig = mergeConfig[tostring(i)]
    end
    this.MaxPetElvLevel = 0;
    for k, v in pairs(evoConfig) do
        if tonumber(k) ~= 0 then
            this.MaxPetElvLevel = this.MaxPetElvLevel + 1
        end
    end
    this.isFirstGetPetGuide = nil
    ListenEvent(Config.EventType.FreashRoleInfoOther, this.refreshOtherPetInfo)
end

function this.setSaveTryPartDataGroup(id, genes)
    this.saveTryPartDataGroup[id] = genes;
    --向服务端申请修改保存的宠物试穿组
end

function this.getSaveTryPartDataGroup(id)
    if id == nil then
        return this.saveTryPartDataGroup;
    else
        return this.saveTryPartDataGroup[id];
    end
end

function this.setBattleShowPetInfo(info)
    this.showBattleFightPetInfo = info;
end

function this.setCurrentExp()
    local iNewExp = this.petExp
    local mergeConfig = G.dataTable["t_elvesmerge"]
    for i = 1, this.petLv, 1 do
        iNewExp = iNewExp - mergeConfig[tostring(i)].f_Exp
    end
    this.CurrentExp = iNewExp

    logicMgr.HeroManager.processAllHeroesPetAttr();
end

function this.GetPetInfoByIndex(listIndex)
    if listIndex > 0 and #this.PetInfoList >= listIndex then
        return this.PetInfoList[listIndex]
    end
    return nil
end
function this.GetPetInfoByPetId(petId)
    for _, v in ipairs(this.PetInfoList) do
        if v.petId == petId then
            return v
        end
    end
    return nil
end

function this.GetAllPartsByType(type)
    if this.AllParts == nil then
        this.AllParts = {};
        local cfg = G.dataTable["t_elvesgene"];
        for i, v in pairs(cfg) do

            if this.AllParts[v.f_ChromosomeId] == nil then
                this.AllParts[v.f_ChromosomeId] = {};
            end
            if v.f_GeneShow == 1 and v.f_ResId ~= "empty" then
                table.insert(this.AllParts[v.f_ChromosomeId], v)
            end
        end
        for i, v in pairs(this.AllParts) do
            table.sort(v, function(a, b)
                return a.f_SeqId > b.f_SeqId
            end)
        end
    end
    return this.AllParts[type];
end

function this.GetNormalPetIndex()
    if this.IsTemplatePet then
        for index, petInfo in ipairs(this.PetInfoList) do
            if this.CheckPetCanTemplate(petInfo) then
                return index
            end
        end
    else
        for index, petInfo in ipairs(this.PetInfoList) do
            if this.CheckPetIsNormal(petInfo) then
                return index
            end
        end
    end
    return -1
end
function this.CheckPetIsNormal(petInfo)
    if petInfo.IsFollowStatus == false
            and petInfo.IsMatingStatus == false
            and petInfo.IsLocked == false
            and petInfo.matingEndTime == 0
            and petInfo.petId ~= this.TemplatePetId
            and petInfo.petId ~= this.MutatePetId
            and petInfo.IsNormalStatus then
        return true
    end
    return false
end
function this.CheckPetCanTemplate(petInfo)
    if petInfo.IsFollowStatus == false
            and petInfo.matingEndTime == 0
            and petInfo.IsMatingStatus == false
            and petInfo.IsLocked == false
            --and petInfo.petId ~= this.TemplatePetId
            and petInfo.petId ~= this.MutatePetId then
        return true
    end
    return false
end
function this.CheckPetCanMerge(petInfo)
    if petInfo.IsMatingStatus == false
            and petInfo.petId ~= this.TemplatePetId
            and petInfo.petId ~= this.MutatePetId
            and petInfo.IsLocked == false
            and petInfo.IsNormalStatus
            and petInfo.IsFollowStatus == false then
        return true
    end
    return false
end

function this.GetPetItemType(petInfo)
    local _petItemType = this.PetItemType.Normal
    if petInfo.IsMatingStatus then
        _petItemType = this.PetItemType.Mating
    elseif petInfo.IsBeautyPageantStatus then
        _petItemType = this.PetItemType.BeautyPageant
    elseif petInfo.matingEndTime > 0 and petInfo.matingEndTime - HelperFunc.GetServerTime() > 0 then
        _petItemType = this.PetItemType.MatingCD
    else
        _petItemType = this.PetItemType.Normal
    end
    return _petItemType
end

function this.GetPetBagSpace()
    return this.MaxPetCount - #this.PetInfoList
end

--获取处于跟随状态的宠物
function this.GetFollowPetInfo()
    for _, v in ipairs(this.PetInfoList) do
        if v.IsFollowStatus == true then
            return v
        end
    end
    return nil
end
--获取战斗中显示的敌方宠物信息
function this.GetShowBattleFightPetInfo()
    return this.showBattleFightPetInfo;
end

function this.GetNewPetInfo(messagePet, playerName)
    local genes = {}
    for _, gene in ipairs(messagePet.genes) do
        table.insert(genes, tonumber(tostring(gene)))
    end
    local geneticMap = {}
    for _, familyTreeGenes in ipairs(messagePet.geneticMap) do
        local _familyTreeGenes = {}
        for _, familyTreeGene in ipairs(familyTreeGenes.genes) do
            table.insert(_familyTreeGenes, tonumber(tostring(familyTreeGene)))
        end
        local _playerName = ""
        local _petIcon = ""
        if #_familyTreeGenes > 0 then
            _playerName = familyTreeGenes.ownerName
            _petIcon = this.GetPetIcon(_familyTreeGenes)
        end
        local _familyTreeDic = GeneticMapStruct.New(_familyTreeGenes, _playerName, _petIcon)
        table.insert(geneticMap, _familyTreeDic)
    end

    local petId = messagePet.petId
    local matingCount = tonumber(tostring(messagePet.matingCount))
    local cupLv = tonumber(tostring(messagePet.cupLv))
    local cupSession = tonumber(tostring(messagePet.cupSession))
    local status = tonumber(tostring(messagePet.status))
    local locked = tonumber(tostring(messagePet.locked))
    local matingEndTime = tonumber(tostring(messagePet.matingEndTime))
    local playerName = playerName
    local petIcon = this.GetPetIcon(genes)
    local _petInfo = PetInfoStruct.New(petId, genes, geneticMap, matingCount, cupLv,
            cupSession, status, locked, matingEndTime, playerName,
            petIcon)
    return _petInfo
end
--新增自己的宠物
function this.AddPet(messagePet)
    local _newPetInfo = this.GetNewPetInfo(messagePet, logicMgr.HeroManager.GetRoleInfo().name)
    table.insert(this.PetInfoList, _newPetInfo)
    table.sort(this.PetInfoList, function(a, b)
        return a.IsFollowStatus;
    end)
    FireEvent(Config.EventType.RefreshPetFollow)
end
function this.AddPets(messagePets)
    for _, pet in ipairs(messagePets) do
        this.AddPet(pet)
    end

    if #this.PetInfoList > 0 and this.CurrentPetId == 0 then
        this.CurrentPetId = this.PetInfoList[1].petId
    end
end
--删除自己的宠物
function this.RemovePetByPetId(petId)
    for i = table.getn(this.PetInfoList), 1, -1 do
        if this.PetInfoList[i].petId == petId then
            local _isFollowStatus = this.PetInfoList[i].IsFollowStatus

            table.remove(this.PetInfoList, i)
            if petId == this.CurrentPetId then
                if #this.PetInfoList > 0 then
                    this.CurrentPetId = this.PetInfoList[1].petId
                else
                    this.CurrentPetId = 0
                end
            end
            if _isFollowStatus then
                if #this.PetInfoList > 0 then
                    PetHandler.ReqPetFollow(this.PetInfoList[1].petId)
                else
                    FireEvent(Config.EventType.RefreshPetFollow)
                end
            end
            return
        end
    end
end
function this.RemovePets(petIds)
    local newPetInfoList = {}
    local isRemove = false
    for _, petInfo in ipairs(this.PetInfoList) do
        isRemove = false
        for _, removePetId in ipairs(petIds) do
            if removePetId == petInfo.petId then
                isRemove = true
            end
        end
        if isRemove == false then
            table.insert(newPetInfoList, petInfo)
        end
    end
    this.PetInfoList = newPetInfoList
    table.sort(this.PetInfoList, function(a, b)
        return a.IsFollowStatus;
    end)
    for _, removePetId in ipairs(petIds) do
        if removePetId == this.CurrentPetId and #this.PetInfoList > 0 then
            this.CurrentPetId = this.PetInfoList[1].petId
            break
        end
    end
end
--添加申请处理(包括自己申请拟变别人宠物 和别的玩家申请拟变我的宠物)
function this.AddApply(apply)
    if tonumber(tostring(apply.toPlayerId)) ~= 0 and tonumber(tostring(apply.fromPlayerId)) ~= 0 then
        local fromPlayerId = apply.fromPlayerId
        local fromPlayerName = tostring(apply.fromPlayerName)
        local fromPlayerPic = apply.fromPlayerPic;
        local fromPlayerPicFrame = apply.fromPlayerPicFrame
        local toPlayerId = apply.toPlayerId
        local toPlayerName = tostring(apply.toPlayerName)
        local toPlayerPic = apply.toPlayerPic
        local toPlayerPicFrame = apply.toPlayerPicFrame
        local endTime = tonumber(tostring(apply.endTime))
        local templatePetInfo = this.GetNewPetInfo(apply.templatePetInfo, toPlayerName)   --模板体
        local mutatePetInfo = this.GetNewPetInfo(apply.mutatePetInfo, fromPlayerName)       --拟变体
        local petMatingApplyInfo = PetMatingApplyInfoStruct.New(templatePetInfo, mutatePetInfo, fromPlayerId, fromPlayerName, fromPlayerPic, fromPlayerPicFrame, toPlayerId,
                toPlayerName, toPlayerPic, toPlayerPicFrame, endTime)
        return petMatingApplyInfo
    end
    return nil
end
function this.AddOthersApply(messageApply)
    local _othersApply = this.AddApply(messageApply)
    if _othersApply ~= nil then
        table.insert(this.applies, _othersApply)
    end
end
function this.AddMyApply(messageApply)
    this.myApply = {}
    local _myApply = this.AddApply(messageApply)
    if _myApply then
        this.myApply = _myApply
    end
end
function this.TryRemoveMyApply(templatePetId, mutatePetId)
    if table.getn(this.myApply) > 0 then
        if templatePetId == this.myApply.templatePetInfo.petId and mutatePetId == this.myApply.mutatePetInfo.petId then
            if mutatePetId == this.MutatePetId then
                this.MutatePetId = 0
            end
            this.SetPetMatingStatus(mutatePetId, false)
            --roleInfoHandler.sendreqOtherRoleInfo(this.myApply.toPlayerId)
            this.myApply = {}
            return true
        end
    end
    return false
end
function this.SetPetMatingStatus(petId, isMating)
    for k, v in ipairs(this.PetInfoList) do
        if v.petId == petId then
            this.PetInfoList[k].IsMatingStatus = isMating
            break
        end
    end
end
function this.RemoveOthersApply(petId, type)
    for i = #this.applies, 1, -1 do
        --所有以我的宠物为模板体的都删除
        if type == 1 then
            --接受以后
            if petId == this.applies[i].templatePetInfo.petId then
                table.remove(this.applies, i)
            end
        else
            --拒绝后单个人取消
            if petId == this.applies[i].mutatePetInfo.petId then
                table.remove(this.applies, i)
            end

        end
    end
end

function this.RemoveOthersApplyByPlayerId(playerId)
    for i = #this.applies, 1, -1 do
        --根据他人ID删除申请列表内申请
        if tostring(this.applies[i].fromPlayerId) == playerId then
            table.remove(this.applies, i)
        end
    end
end

function this.TryChangePetMatingEndTime(petId, matingEndTime)
    for _, v in ipairs(this.PetInfoList) do
        if v.petId == petId then
            v.matingCount = v.matingCount + 1
            v.matingEndTime = matingEndTime
            return true
        end
    end
    return false
end

--客户端倒计时
function this.PetMatingCountDown()
    --我自己的宠物有处于繁殖期
    for _, petInfo in ipairs(this.PetInfoList) do
        if petInfo.matingEndTime > 0 and (HelperFunc.GetServerTime() - petInfo.matingEndTime) > 0 then
            petInfo.matingEndTime = 0
            petInfo.IsMatingStatus = false
        end
    end
    --我的申请过期
    if table.getn(this.myApply) > 0 then
        if this.myApply.mutatePetInfo then
            if this.myApply.mutatePetInfo.IsMatingStatus
                    and (this.myApply.mutatePetInfo.matingEndTime > 0 and this.myApply.mutatePetInfo.matingEndTime - HelperFunc.GetServerTime() > 0)
                    and (HelperFunc.GetServerTime() - this.myApply.endTime) > 0 then
                this.MutatePetId = 0
                this.SetPetMatingStatus(this.myApply.mutatePetInfo.petId, false)
                this.myApply = {}
            end
        end
    end
    --他人申请过期
    if this.applies then
        for i = #this.applies, 1, -1 do
            --模板体处于繁殖期
            if this.applies[i].templatePetInfo then
                if this.applies[i].templatePetInfo.IsMatingStatus
                        or (this.applies[i].templatePetInfo.matingEndTime > 0 and this.applies[i].templatePetInfo.matingEndTime - HelperFunc.GetServerTime() > 0)
                        or (HelperFunc.GetServerTime() - this.applies[i].endTime) > 0 then
                    table.remove(this.applies, i)
                end
            end
        end
    end
end

--宠物名字通过拼接产生
function this.GetPetName(genes)
    if genes and #genes > 0 then
        local _geneConfig = G.dataTable["t_elvesgene"]
        local _bodyInfo = _geneConfig[tostring(genes[1])]
        local _bodyName = getLanguage(_bodyInfo.f_Name)
        return _bodyName
    end
    return ""
end
--宠物图片
function this.GetPetIcon(genes)
    if genes and #genes then
        local _geneConfig = G.dataTable["t_elvesgene"]
        local _bodyInfo = _geneConfig[tostring(genes[1])]
        if _bodyInfo then
            return "ui://PetAtlas/" .. _bodyInfo.f_GeneId
        end
    end
    return ""
end
function this.GetPetIconByGeneInfo(geneInfo)
    return "ui://PetAtlas/" .. geneInfo.f_ResId
end
function this.GetTalentBuffIcon(talentBuffType)
    local _buffIcon = ""
    if talentBuffType == this.TalentBuffType.BattleBuff then
        _buffIcon = this.TalentBattleBuffIcon
    elseif talentBuffType == this.TalentBuffType.EcoBuff then
        _buffIcon = this.TalentEcoBuffIcon
    end
    return "ui://CommonUI/" .. _buffIcon
end
function this.GetMutationLevelFrameIcon(mutationLevel)
    local _iconName = ""
    if mutationLevel == 1 then
        _iconName = this.MutationLevelFrameIcon1
    elseif mutationLevel == 2 then
        _iconName = this.MutationLevelFrameIcon2
    end
    return "ui://ICONAtlas/" .. _iconName
end
--获取宠物突变等级
function this.GetMutationLevel(genes)
    local _level = 1
    if genes and #genes > 0 then
        local _geneConfig = G.dataTable["t_elvesgene"]
        for _, geneId in pairs(genes) do
            local _geneInfo = _geneConfig[tostring(geneId)]
            if _geneInfo.f_MutationLevel > _level then
                _level = _geneInfo.f_MutationLevel
            end
        end
    end
    return _level
end

--获取自己的宠物拟变时间
function this.GetPetMatingEndTime(petId)
    for _, petInfo in ipairs(this.PetInfoList) do
        if petInfo.petId == petId then
            return petInfo.matingEndTime
        end
    end
    return HelperFunc.GetServerTime()
end

--获取宠物资源Buff列表
function this.GetPetEcoBuffList()
    local _buffList = {}
    local _ecoBuffIds = G.dataTable["t_elvesmerge"][tostring(this.petLv)]["f_EcoBuffId"]
    if not string.IsNilOrEmpty(_ecoBuffIds) then
        for _, ecoBuffId in ipairs(_ecoBuffIds) do
            local _ecoBuffInfo = G.dataTable["t_ecobuff"][tostring(ecoBuffId)]
            if _ecoBuffInfo then
                local _newInfo = {}
                _newInfo.buffId = _ecoBuffInfo.f_EcoBuff
                _newInfo.buffDesc = _ecoBuffInfo.f_BuffDesc
                _newInfo.buffIcon = PetManager.GetTalentBuffIcon(PetManager.TalentBuffType.EcoBuff)
                table.insert(_buffList, _newInfo)
            end
        end
    end
    return _buffList
end

function this.GetPetEcoBuffIds()
    if this.petLv == 0 or this.petLv == nil then
        return {};
    end
    local EcoBuffIds = decodeJsonStrToTabel(G.dataTable["t_elvesmerge"][tostring(this.petLv)]["f_EcoBuffId"])
    return EcoBuffIds;
end

----------------------------------------------------------------------------
--处理服务器协议
--初始化玩家自己的宠物
function PetManager.ResPets(msg)
    this.init()
    BrokerManager.Init()
    --宠物列表
    this.PetInfoList = {}
    this.AddPets(msg.pets)

    --别的玩家申请拟变我的宠物
    this.applies = {}
    for _, apply in ipairs(msg.applies) do
        this.AddOthersApply(apply)
    end

    --我申请拟变别的玩家宠物
    this.AddMyApply(msg.myApply)

    this.petLv = tonumber(tostring(msg.petLv))
    TAManager.UserSet("pet_level", this.petLv)
    local mergeConfig = G.dataTable["t_elvesmerge"]

    local sumExp = 0;
    for i = 1, msg.petLv do
        sumExp = sumExp + mergeConfig[tostring(i)].f_Exp;
    end
    sumExp = sumExp + tonumber(tostring(msg.petExp));

    this.petExp = sumExp;
    this.EvoLevelExp = tonumber(tostring(msg.petEvolveExp))
    this.EvoLevel = tonumber(tostring(msg.petEvolveLv))
    this.setCurrentExp()
    logicMgr.EcoBuffManager.updateEcoBuff(EnumConst.EcoBuffType.Pet, this.GetPetEcoBuffIds());
    FireEvent(Config.EventType.RefreshPetList)

    TAManager.UserSet("pet_level", logicMgr.PetManager.petLv)
    TAManager.UserSet("elves_level", logicMgr.PetManager.EvoLevel)
end

function this.ResAddPet(msg)
    if msg.pets then
        this.NewAddPetIds = {}
        local oldCount = table.getn(this.PetInfoList)
        for _, pet in ipairs(msg.pets) do
            table.insert(this.NewAddPetIds, pet.petId)
            this.AddPet(pet)

            local _petDic = {}
            _petDic.pet_id = tostring(pet.petId)
            _petDic.gene_list = getJsonStr(pet.genes)
            _petDic.is_item = -1
            _petDic.item_id = -1
            _petDic.mutate_pet_id = -1
            _petDic.template_pet_id = -1
            TAManager.TrackByDictionary("elves_birth", _petDic)
        end

        if this.CurrentPetId == 0 and #this.PetInfoList > 0 then
            this.CurrentPetId = this.PetInfoList[1].petId
        end

        local _followPetInfo = this.GetFollowPetInfo()
        if _followPetInfo and _followPetInfo.petId == this.PetInfoList[1].petId then
            --FireEvent(Config.EventType.RefreshPetFollow)
        end
        if oldCount == 0 then
            this.isFirstGetPetGuide = true
        end
        FireEvent(Config.EventType.RefreshPetList)
        showUI(PanelNames.Page_PetMainAddPets)
    end
end
function this.ResGetFirstPet(message)
    if message.firstPet then
        this.NewAddPetIds = {}
        local _newPetId = message.firstPet.petId
        table.insert(this.NewAddPetIds, _newPetId)
        this.AddPet(message.firstPet)

        local _petDic = {}
        _petDic.pet_id = tostring(_newPetId)
        _petDic.gene_list = getJsonStr(message.firstPet.genes)
        _petDic.is_item = false
        _petDic.item_id = -1
        _petDic.mutate_pet_id = -1
        _petDic.template_pet_id = -1
        TAManager.TrackByDictionary("elves_birth", _petDic)

        this.CurrentPetId = _newPetId
        FireEvent(Config.EventType.RefreshPetFollow)
        FireEvent(Config.EventType.RefreshPetList)
        showUI(PanelNames.Page_PetMainAddPets)
    end
end

function this.ResLockPet(petId, isLoacked)
    for k, v in ipairs(this.PetInfoList) do
        if v.petId == petId then
            this.PetInfoList[k].IsLocked = isLoacked
            break
        end
    end
    FireEvent(Config.EventType.RefreshPetLock)
end

function this.ResPetFollow(petId, isFollow)
    for k, v in ipairs(this.PetInfoList) do
        if v.petId == petId then
            this.PetInfoList[k].IsFollowStatus = isFollow
            break
        end
    end
    table.sort(this.PetInfoList, function(a, b)
        return a.IsFollowStatus;
    end)

    if isFollow then
        FireEvent(Config.EventType.RefreshPetFollow)
    end
end

function this.ResSelfMating(msg)
    local _newEvoLevel = tonumber(tostring(msg.petEvolveLv));
    local _newEvolveExp = tonumber(tostring(msg.petEvolveExp))
    local _petDic = {}
    _petDic.new_evolve_lv = _newEvoLevel
    _petDic.old_evolve_lv = this.EvoLevel
    _petDic.new_evolve_exp = _newEvolveExp
    _petDic.old_evolve_exp = this.EvoLevelExp
    _petDic.evolve_type = "myself"
    _petDic.mutate_pet_id = tostring(this.MutatePetId)
    _petDic.template_pet_id = tostring(this.TemplatePetId)
    _petDic.new_pet_id = tostring(msg.newPet.petId)
    TAManager.TrackByDictionary("pet_evolve", _petDic)

    if this.EvoLevel < _newEvoLevel then
        --宠物进化等级升级
        this.isEvoLvUp = 1;
    end
    this.lockParts = {}

    -- 删除拟变体
    this.RemovePetByPetId(this.MutatePetId)
    this.EvoLevel = _newEvoLevel;
    this.EvoLevelExp = _newEvolveExp
    local _mutateDic = {}
    _mutateDic.pet_id = tostring(this.MutatePetId)
    _mutateDic.reasons = "mutate"
    _mutateDic.info_ex = -1
    TAManager.TrackByDictionary("elves_consumed", _mutateDic)
    this.MutatePetId = 0
    -- 模板体冷却
    for k, v in ipairs(this.PetInfoList) do
        if v.petId == this.TemplatePetId then
            this.PetInfoList[k].matingCount = this.PetInfoList[k].matingCount + 1
            this.PetInfoList[k].matingEndTime = tonumber(tostring(msg.endTime)) / 1000
            break
        end
    end
    this.TemplatePetId = 0

    -- 添加新出生的petInfo
    this.AddPet(msg.newPet)
    this.NewBornPetId = msg.newPet.petId
    AudioManager.Play(2019)

    FireEvent(Config.EventType.RefreshPetList)
    FireEvent(Config.EventType.RefreshPetMating)
    showUI(PanelNames.Page_PetMatingSuccess, 0)
end

function this.ResMergePet(msg)
    local _newPetLv = tonumber(tostring(msg.petLv))
    local mergeConfig = G.dataTable["t_elvesmerge"]
    local sumExp = 0;
    for i = 1, msg.petLv do
        sumExp = sumExp + mergeConfig[tostring(i)].f_Exp;
    end
    sumExp = sumExp + tonumber(tostring(msg.petExp));

    if this.petLv < _newPetLv then
        showUI(PanelNames.UIPetLevelUp, _newPetLv)
        -- 牧场升级音效
        AudioManager.Play(2017)
        --AudioManager.Play(2025)
    else
        -- AudioManager.Play(2025)
        -- 飞飞融合音效
        -- AudioManager.Play(2018)
    end
    --飞飞被消耗后 TA
    for _, petId in ipairs(msg.petIds) do
        local _petDic = {}
        _petDic.pet_id = tonumber(petId)
        _petDic.reasons = "merge"
        _petDic.info_ex = -1
        TAManager.TrackByDictionary("elves_consumed", _petDic)
    end

    -- 删除融合列表中的pet
    this.RemovePets(msg.petIds)
    this.petLv = _newPetLv;
    this.petExp = sumExp;
    this.setCurrentExp()
    logicMgr.EcoBuffManager.updateEcoBuff(EnumConst.EcoBuffType.Pet, this.GetPetEcoBuffIds());
    FireEvent(Config.EventType.RefreshPetList)
    FireEvent(Config.EventType.RefreshMergePet)
end
--拒绝支援拟变返回
function this.ResRefuseMating(message)
    local _templatePetId = message.tmplPetId
    local _mutatePetId = message.mutatePetId
    --我与他人宠物进行的拟变
    local _isMyApply = this.TryRemoveMyApply(_templatePetId, _mutatePetId)
    if _isMyApply == true then
        FireEvent(Config.EventType.RefreshMyApply)
    else
        --他人与我的宠物进行拟变
        this.RemoveOthersApply(_mutatePetId, 2)
        FireEvent(Config.EventType.RefreshOthersApplyList)
    end
    this.myApply = {}
end
--同意支援拟变返回
function this.ResAgreeMating(message)
    local _templatePetId = message.tmplPetId
    local _mutatePetId = message.mutatePetId
    --拟变体是我的宠物
    local _isMyApply = this.TryRemoveMyApply(_templatePetId, _mutatePetId)
    if _isMyApply then
        FireEvent(Config.EventType.RefreshMyApply)
    else
        --模板体是我的宠物
        local _endTime = tonumber(tostring(message.endTime)) / 1000 + 86400;
        --模板体是我的宠物
        local _isMyTemplate = this.TryChangePetMatingEndTime(_templatePetId, math.floor(_endTime))
        this.RemoveOthersApply(_templatePetId, 1)
        FireEvent(Config.EventType.RefreshOthersApplyList)
    end
end
--取消支援拟变返回
function this.ResCancelMating(message)
    local _templatePetId = message.tmplPetId
    local _mutatePetId = message.mutatePetId
    --我与他人宠物进行的拟变
    local _isMyApply = this.TryRemoveMyApply(_templatePetId, _mutatePetId)
    this.RemoveOthersApply(_templatePetId, 1)
    --我自己的两只宠物进行拟变
    if _isMyApply == false then
        if _templatePetId == this.TemplatePetId then
            this.TemplatePetId = 0
        end
        if _mutatePetId == this.MutatePetId then
            this.MutatePetId = 0
        end
    end
    FireEvent(Config.EventType.RefreshPetCancelMating)
    this.myApply = {}
end
--支援拟变成功返回
function this.ResMatingSuccess(message)
    local _templatePetId = message.tmplPetId
    local _mutatePetId = message.mutatePetId
    local _endTime = tonumber(tostring(message.endTime))
    --模板体是我的宠物
    local _isMyTemplate = this.TryChangePetMatingEndTime(_templatePetId, _endTime)
    if _isMyTemplate then
        FireEvent(Config.EventType.RefreshPetMating)
    else
        --拟变体是我的宠物
        this.RemovePetByPetId(_mutatePetId)
        local _mutatePetDic = {}
        _mutatePetDic.pet_id = tostring(_mutatePetId)
        _mutatePetDic.reasons = "mutate"
        _mutatePetDic.info_ex = -1
        TAManager.TrackByDictionary("elves_consumed", _mutatePetDic)

        -- 添加新出生的petInfo
        this.AddPet(message.newPet)
        this.NewBornPetId = message.newPet.petId

        local _petDic = {}
        _petDic.new_pet_id = tostring(message.newPet.petId)
        _petDic.mutate_pet_id = tostring(message.tmplPetId)
        _petDic.template_pet_id = tostring(message.mutatePetId)
        _petDic.new_evolve_exp = this.EvoLevelExp
        _petDic.old_evolve_exp = this.EvoLevelExp
        _petDic.new_evolve_lv = this.EvoLevel
        _petDic.old_evolve_lv = this.EvoLevel
        _petDic.evolve_type = "friend"
        TAManager.TrackByDictionary("pet_evolve", _petDic)

        local _templatePetId = message.tmplPetId
        local _mutatePetId = message.mutatePetId
        --拟变体是我的宠物
        local _isMyApply = this.TryRemoveMyApply(_templatePetId, _mutatePetId)
        if _isMyApply then
            FireEvent(Config.EventType.RefreshMyApply)
        end
        AudioManager.Play(2019)

        FireEvent(Config.EventType.RefreshPetList)
        FireEvent(Config.EventType.RefreshPetMating)
        showUI(PanelNames.Page_PetMatingSuccess, 1)
    end
end
--支援拟变失败返回
function this.ResMatingFailed(message)
    local _templatePetId = message.tmplPetId
    local _mutatePetId = message.mutatePetId
    --拟变体是我的宠物
    local _isMyApply = this.TryRemoveMyApply(_templatePetId, _mutatePetId)
    if _isMyApply then
        FireEvent(Config.EventType.RefreshMyApply)
    else
        --模板体是我的宠物
        this.RemoveOthersApply(_templatePetId, 1)
        FireEvent(Config.EventType.RefreshOthersApplyList)
    end
end
--宠物支援拟变返回(我发起的)
function this.ResAskMating(message)
    --拟变体是我的宠物
    if message.myApply and #message.myApply > 0 then
        local _mutatePetId = message.myApply[1].mutatePetInfo.petId
        local _mutatePetInfo = this.GetPetInfoByPetId(_mutatePetId)
        this.SetPetMatingStatus(_mutatePetId, math.floor(message.myApply[1].mutatePetInfo.status / 8) == 1)

        if _mutatePetInfo then
            this.TemplatePetId = 0
            this.MutatePetId = 0
            this.AddMyApply(message.myApply[1])
            FireEvent(Config.EventType.RefreshMyApply)
        end
    end
end
--收到支援拟变请求通知(别人发起)
function this.ResApplyMating(message)
    --模板体是我的宠物
    if message.apply and message.apply.templatePetInfo and message.apply.templatePetInfo.petId then
        local _templatePetInfo = message.apply.templatePetInfo
        local _templatePetInfo = this.GetPetInfoByPetId(_templatePetInfo.petId)
        if _templatePetInfo then
            this.AddOthersApply(message.apply)
            FireEvent(Config.EventType.RefreshOthersApplyList)
        end
    end
end

--更新他人的宠物信息
function this.refreshOtherPetInfo(info)
    this.OtherPetInfo = {}
    if info.data.pet and #info.data.pet > 0 then
        local _otherPetInfo = this.GetOtherPetInfo(info.data.pet[1], info.data.playerId, info.data.name, info.data.pic, info.data.frame ~= nil and info.data.frame or info.data.picFrame)
        this.OtherPetInfo = _otherPetInfo
        FireEvent(Config.EventType.RefreshOtherPetInfo)
    end
end
function this.GetOtherPetInfo(messageOtherPetInfo, playerId, playerName, playerPic, playerFramePic)
    local _genes = {}
    for _, gene in pairs(messageOtherPetInfo.genes) do
        table.insert(_genes, tonumber(tostring(gene)))
    end
    local _geneticMap = {}
    for _, geneticMap in pairs(messageOtherPetInfo.geneticMap) do
        local _familyTreeGenes = {}
        for _, familyTreeGene in pairs(geneticMap.genes) do
            table.insert(_familyTreeGenes, tonumber(tostring(familyTreeGene)))
        end
        local _familyTreeDic = GeneticMapStruct.New(_familyTreeGenes, geneticMap.ownerName, this.GetPetIcon(_familyTreeGenes))
        table.insert(_geneticMap, _familyTreeDic)
    end
    local _petId = messageOtherPetInfo.petId
    local _createTime = tonumber(tostring(messageOtherPetInfo.createTime))
    local _matingCount = tonumber(tostring(messageOtherPetInfo.matingCount))
    local _cupLv = tonumber(tostring(messageOtherPetInfo.cupLv))
    local _cupSession = tonumber(tostring(messageOtherPetInfo.cupSession))
    local _matingEndTime = tonumber(tostring(messageOtherPetInfo.matingEndTime))
    local _petIcon = this.GetPetIcon(_genes)
    local _otherPetInfo = OtherPetInfoStruct.New(_petId, _createTime, _matingCount, _cupLv, _cupSession,
            _matingEndTime, _genes, _geneticMap, playerId, playerName,
            _petIcon, playerPic, playerFramePic)
    return _otherPetInfo
end

--显示宠物模型
--第一次设置 modelInfo{modelGO,parentGO,modelWrapper,petId}
function this.PetModelFirstSet(modelInfo, genes, isLoop, localPosition, localScale, stencilComp)
    if modelInfo.modelGO and #genes == PetManager.PetPart.Ornament then
        modelInfo.modelGO:SetActive(true)
        modelInfo.modelGO:AddComponent(TCanvas)
        modelInfo.modelGO:AddComponent(TChangeSortingOrder)
        modelInfo.modelWrapper:SetWrapTarget(modelInfo.modelGO, false)
        modelInfo.parentGO:SetNativeObject(modelInfo.modelWrapper)
        Util.Lua_SetLocalPositionXYZ(modelInfo.modelGO, localPosition.x, localPosition.y, localPosition.z)
        Util.Lua_SetLocalScaleXYZ(modelInfo.modelGO, localScale.x, localScale.y, localScale.z)
        local _skeletonGraphic = modelInfo.modelGO:GetComponent("SkeletonGraphic")
        this.changePetSlot(_skeletonGraphic, genes, isLoop)
    end
end

--第n次刷新
function this.PetModelRefresh(modelInfo, genes, isLoop, stencilComp)
    if modelInfo.modelGO and #genes == PetManager.PetPart.Ornament then
        local _skeletonGraphic = modelInfo.modelGO:GetComponent("SkeletonGraphic")
        if _skeletonGraphic then
            this.changePetSlot(_skeletonGraphic, genes, isLoop)
            return
        end
    end
end
function this.changePetSlot(skeletonGraphic, genes, isLoop)
    if not isLoop then
        skeletonGraphic.timeScale = 0.00001
    end
    --skeletonGraphic.freeze = not isLoop
    local _geneConfig = G.dataTable["t_elvesgene"]
    for _, petSlot in pairs(PetManager.PetSlot) do
        local _geneId = genes[petSlot.Part]
        local _geneInfo = _geneConfig[tostring(_geneId)]
        --不是默认皮肤
        local _skinIndex = _geneInfo.f_SeqId
        local _skin = skeletonGraphic.Skeleton.Data:FindSkin("skin" .. tostring(_skinIndex))
        if _skin == nil then
            _skin = skeletonGraphic.Skeleton.Data:FindSkin("skin1")
        end
        local _attachment = _skin:GetAttachment(petSlot.Index, petSlot.Slot)
        if _attachment then
            skeletonGraphic.Skeleton:SetAttachment(petSlot.Slot, _attachment, false)
        end
    end
    skeletonGraphic.startingLoop = isLoop
    skeletonGraphic:Refresh()
end
function this.LoadItemPetModel(in_petInfo,in_modelList,in_index,in_parentGO)
    if nil == in_modelList[in_index] then
        in_modelList[in_index] = {}
    end
    if in_modelList[in_index].modelGO
        and in_modelList[in_index].petId == in_petInfo.petId then
            PetManager.PetModelRefresh(in_modelList[in_index], in_petInfo.genes, false)
    else
        --删除model本身
        if in_modelList[in_index].modelGO then
            destroyImmediate(in_modelList[in_index].modelGO)
        end
        in_modelList[in_index].modelGO = nil
        in_modelList[in_index].parentGO = nil
        in_modelList[in_index].petId = nil
        --重新加载模型
        resMgr:LoadModelPrefab(PetManager.PetModelPrefab, function(go)
            local _petGO = newObject(go)
            local _skeletonGraphicComp = _petGO:GetComponent("SkeletonGraphic")
            if _petGO and _skeletonGraphicComp then
                _skeletonGraphicComp.material:SetFloat("_StencilComp", 3)
                _skeletonGraphicComp.material:SetFloat("_Stencil", 1);
            end

            in_modelList[in_index].modelGO = _petGO
            in_modelList[in_index].parentGO = in_parentGO
            local _modelWrapper = in_modelList[in_index].modelWrapper
            in_modelList[in_index].modelWrapper = GoWrapper.New()
            in_modelList[in_index].petId = in_petInfo.petId
            PetManager.PetModelFirstSet(in_modelList[in_index], in_petInfo.genes, false, Vector3.New(0, 0, 0), Vector3.New(0.55, 0.55, 1), 3)
            if _modelWrapper then
                _modelWrapper:Dispose()
            end
        end)
    end
end
function this.RemovePetModelTable(in_petModelTable)
    for key, petModelItem in pairs(in_petModelTable) do
        if petModelItem.modelGO then
            destroyImmediate(petModelItem.modelGO)
        end
        petModelItem.modelGO = nil
        petModelItem.parentGO = nil
        petModelItem.petId = nil
        petModelItem = nil
    end
    in_petModelTable = nil
end

--检查该类型是否锁定
function this.checkTypeIsCheck(type)
    for i, v in pairs(this.lockParts) do
        if v.type == type then
            return true;
        end
    end
    return false;
end


--宠物界面 宠物列表中每个item对应的属性控制type
PetManager.PetItemType = {
    Normal = 0,
    --Loacked = 1,
    Mating = 1,
    --Following = 3, --挂牌中
    BeautyPageant = 2, --添加
    MatingCD = 3, --繁殖冷却中
}
--宠物部件Enum
PetManager.PetPart = {
    Body = 1,
    Eyes = 2,
    Eyebrow = 3, --眉毛
    NoseAndMouth = 4,
    Beard = 5, --胡须

    Hair = 6,
    Ear = 7,
    Tail = 8, --尾巴
    Wing = 9,
    Headwear = 10, --头部饰品

    Upperwear = 11, --上身饰品
    Handwear = 12, --手部饰品
    Ornament = 13, --空中饰品
}
--每次重新导入pet资源时，请确认index的对应关系
PetManager.PetSlot = {
    { Part = 1, Index = 6, Slot = "Body" }, --身体
    { Part = 1, Index = 10, Slot = "Body_a" }, --头
    { Part = 1, Index = 8, Slot = "Body_b" }, --左胳膊
    { Part = 1, Index = 2, Slot = "Body_c" }, --右胳膊
    { Part = 1, Index = 5, Slot = "Body_d" }, --左腿
    { Part = 1, Index = 4, Slot = "Body_f" }, --右腿
    { Part = 2, Index = 14, Slot = "Eyes" }, --眼睛
    { Part = 3, Index = 15, Slot = "Eyebrow" }, --眉毛
    { Part = 4, Index = 12, Slot = "NoseAndMouth" }, --鼻子和嘴巴
    { Part = 5, Index = 11, Slot = "Beard" }, --胡须
    { Part = 6, Index = 16, Slot = "Hair" }, --毛发
    { Part = 7, Index = 13, Slot = "Ear" }, --耳朵
    { Part = 7, Index = 7, Slot = "Ear_a" }, --
    { Part = 8, Index = 1, Slot = "Tail" }, --尾巴
    { Part = 9, Index = 0, Slot = "Wing" }, --翅膀
    { Part = 10, Index = 17, Slot = "Headwear" }, --头饰
    { Part = 11, Index = 9, Slot = "Upperwear" }, --上身饰品
    { Part = 12, Index = 3, Slot = "Handwear" }, --手部饰品
    { Part = 13, Index = 18, Slot = "Ornament" }, --空中饰品
}

PetManager.FamilyTree = {
    MySelf = 0,
    Father = 1,
    Mother = 2,
    Grandfather = 3,
    Grandmother = 4,
    Grandpa = 5, --外公
    Grandma = 6, --外婆
}

--宠物天赋的buff类型(对应配置表t_elvestalent.f_TalentBuffType)
PetManager.TalentBuffType = {
    BattleBuff = 1, --战斗天赋
    EcoBuff = 2, --资源天赋
}

PetManager.PetModelPrefab = "Model/Pet/pet_100001"
PetManager.PetModel2Prefab = "Model/Pet/pet_100002"
PetManager.TalentBattleBuffIcon = "pet_talenttype_icon_01"
PetManager.TalentEcoBuffIcon = "pet_talenttype_icon_02"
PetManager.PetLeftPosition = Vector3.New(-120, 150, 0)
PetManager.PetRightPosition = Vector3.New(120, 150, 0)
PetManager.PetLeftRotation = Vector3.New(0, 0, 0)
PetManager.PetRightRotation = Vector3.New(0, 180, 0)
PetManager.MutationLevelFrameIcon1 = "icon_frame_04_01"
PetManager.MutationLevelFrameIcon2 = "icon_frame_06_01"
PetManager.NewPetSpecialEffectDuration = 2800
--魂印
function this.PartListInfo(data)
    this.partAttrData = {}
    this.partAttrData = data
    this.SetPartAllLv()
    this.Track_PetPartLevel()
end

function this.PetPartData()
    if not petPartSkillByGroup then
        this.petPartSkillByGroup = {}
    end
    for i=1,18 do
        local cfg = this.petPartTBSkill[tostring(i)]
        if not this.petPartSkillByGroup[cfg.f_PetSkillGroup] then
            this.petPartSkillByGroup[cfg.f_PetSkillGroup] = cfg
        else
            if this.petPartAllLv >= cfg.f_PetAttributeTotalLevel then
                this.petPartSkillByGroup[cfg.f_PetSkillGroup] = cfg
            end
        end
    end
end

function this.UpdateOnePart(oneData)
    local isHas = nil
    for _,v in ipairs(this.partAttrData) do
        if v.partId == oneData.partId then
            v.partLv = oneData.partCurrLv
               isHas = true
            break
        end
    end
    if not isHas then
        table.insert(this.partAttrData,{["partId"] = oneData.partId,["partLv"] = oneData.partCurrLv})
    end
    this.SetPartAllLv()
    this.Track_PetPartLevel()
end

--飞飞灵饰属性埋点
function this.Track_PetPartLevel()
    local tb = {}
    for _,v in pairs(this.partAttrData) do
        tb[v.partId] = v
    end
    for i=1, 13 do
        if not tb[i] then
            tb[i] = {partId = i, partLv = 0}
        end
    end
    logicMgr.TAManager.UserSet("pet_part_level", getJsonStr(tb))
end

function this.PetPartAddAttr()
    local attrs = {}
    for _,v in pairs(this.partAttrData) do
        if v then
            local cfg = this.petPartTBdata[tostring(v["partId"] * 1000 + v["partLv"])]
            if cfg then
                if attrs[cfg.f_AttributeType] then
                    attrs[cfg.f_AttributeType] = attrs[cfg.f_AttributeType] + cfg.f_AttributeValue
                else
                    attrs[cfg.f_AttributeType] = cfg.f_AttributeValue
                end
            end
        end
    end
    return attrs
end

function this.IsCanUp()
    for _,v in pairs(this.petPartTBdata) do
        local itemNum = logicMgr.ItemManager.getItemCountbyModelId(v.f_GrowthCost[1])
        if this.petPartAllLv >= v.f_NeedTotalLevel and itemNum >= v.f_GrowthCost[2] then
            return true
        end
    end
    return false
end

function this.GetCurSkill()
    for _,v in pairs(this.petPartTBSkill) do
        if v.f_PetAttributeTotalLevel == this.petPartAllLv then
            return v
        end
    end
    return false
end

function this.GetCurPartdata(partId,isbefore)
    local lv,partCfg,afterCfg = 0,{},{}
    if #this.partAttrData > 0 then
        for _,v in ipairs(this.partAttrData) do
            if v.partId == partId then
                lv = v.partLv
                break
            end
        end
    end
    if isbefore then
        partCfg = this.petPartTBdata[tostring(partId*1000+lv-1)]
    else
        partCfg = this.petPartTBdata[tostring(partId*1000+lv)]
    end
    if partCfg.f_GrowthCost ~= "" then
        afterCfg = this.petPartTBdata[tostring(partId*1000+lv+1)]
    end
    return partCfg,afterCfg
end

function this.SetPartAllLv()
    this.petPartAllLv = 0
    if #this.partAttrData > 0 then
        for _,v in ipairs(this.partAttrData) do
            this.petPartAllLv = this.petPartAllLv + v.partLv
        end
    end
    this.PetPartData()
end

function this.RestBackItems(items)
    showUI(PanelNames.UICommonItemGet,nil,nil,2,items,nil,nil,nil,nil,nil,nil,nil,getLanguage("WeaponReset"))
end
----------------------------埋点 s
PetManager.Track_buyPet = function(pid, genes)
    local dic = {pid = pid, gene_list = getJsonStr(genes)}
    TAManager.TrackByDictionary("buy_pet", dic)
end

PetManager.Track_evolvePet = function(pid, genes)
    local dic = {pid = pid, gene_list = getJsonStr(genes)}
    TAManager.TrackByDictionary("evolve_pet", dic)
end

PetManager.Track_putawayPet = function(genes, sell, evo)
    local stime = GetPlayerPrefs("putaway_pet")
    local ts = os.date("%Y-%m-%d", HelperFunc.GetServerTime(true))
    local first = 1
    if ts == stime then
        first = 0
    end

    local dic = {gene_list = getJsonStr(genes), sell_price = sell, evo_price = evo, is_first = first}
    TAManager.TrackByDictionary("putaway_pet", dic)
    SetPlayerPrefs("putaway_pet", ts)
end
----------------------------埋点 e

return PetManager